import pymysql

class Job:
    def __init__(self):
        self.db = pymysql.connect(  
            host='localhost',  # 数据库服务器地址，本地数据库使用'localhost'  
            user='root',  # 数据库用户名  
            password='123456',  # 数据库密码  
            database='jobdata',  # 数据库名称  
            charset= 'utf8mb4'
        ) 
        self.cursor = self.db.cursor()
    
    def get_job(self,info_id):
        # 获取所有数据
        self.cursor.execute('select jobtitle.name,jobtitle.experience,jobtitle.ltd,jobtitle.price,jobinfo.descriptions,jobinfo.tags,jobinfo.address from jobinfo right join jobtitle on jobinfo.info_id = jobtitle.infoid where jobinfo.info_id = %s',(info_id))

        data = self.cursor.fetchone()

        try:
            dic = {
                'jobname': data[0],
                'experience': data[1],
                'ltd': data[2],
                'price': data[3],
                'descriptions': data[4],
                'tags': data[5].split('\n') if len(data[5].split('\n')) != 1 else list(eval(data[5])),
                'address': data[-1]
            }

            return {'code':200,'result':dic}
    
        except:
            return {'code':100,'result':"岗位已下架"}

